const fs = eval('require')('fs');
const path_ = eval('require')('path');
const eventClass = require('./eventClass');

/** *
 * 各个方法传入参数均为以下格式(具体参数依照事件对待)
 * cb 回调函数
 * obj = {
 *      fileName: '文件名',
 *      content: '内容',
 *      binary: true // 使用二进制形式
 * }
 */
class fileClass extends eventClass {
    constructor() {
        super('fileClass');
    }

    append(cb, { fileName, content, binary = null }) {
        const this_ = this;
        fs.appendFile(fileName, content, (err) => {
            this_.returnMethod(cb, {
                err,
                data: '添加成功',
            });
        });
    }

    write(cb, { fileName, content, binary }) {
        let ret = '写入成功';
        let err = null;
        try {
            if (binary) {
                fs.writeFileSync(fileName, content, 'utf-8', 'binary');
            } else {
                fs.writeFileSync(fileName, content, 'utf-8');
            }
        } catch (e) {
            err = e;
            ret = '写入失败';
        } finally {
            this.returnMethod(cb, {
                err,
                data: ret,
            });
        }
    }

    read(cb, { fileName, content = null, binary }) {
        let ret = null;
        let err = null;
        try {
            if (binary) {
                ret = fs.readFileSync(fileName, 'utf-8', 'binary');
            } else {
                ret = fs.readFileSync(fileName, 'utf-8');
            }
        } catch (e) {
            err = e;
        } finally {
            this.returnMethod(cb, {
                err,
                data: ret,
            });
        }
    }

    exists(cb, { fileName, content = null, binary = null }) {
        let data = '';
        fileName = path_.resolve(fileName);
        if (fs.existsSync(fileName)) {
            if (fs.statSync(fileName).isDirectory()) {
                data = 'folder';
            } else {
                data = 'file';
            }
        } else {
            data = false;
        }
        this.returnMethod(cb, {
            err: null,
            data,
        });
    }

    /**
     * 判断文件是否存在，存在则读，不存在则写
     * 默认为文本文件的操作
     * */
    existsWithReadOrCreate(cb, { fileName, content }) {
        if (fs.existsSync(fileName)) {
            if (fs.statSync(fileName).isDirectory()) {
                this.returnMethod(cb, { err: '该路径不是一个文件的路径' });
            } else {
                this.returnMethod(cb, { data: fs.readFileSync(fileName, 'utf-8') });
            }
        } else {
            fs.writeFileSync(fileName, content, 'utf-8');
            this.returnMethod(cb, { data: content });
        }
    }

    copyFile(cb, { source, target }) {
        const this_ = this;
        source = path_.resolve(source);
        target = path_.resolve(target);
        fs.copyFile(source, target, (err) => {
            if (err) {
                this_.returnMethod(cb, { err, data: '复制失败' });
            } else {
                this_.returnMethod(cb, { err: null, data: '复制成功' });
            }
        });
    }

    copyFileWithReplace(cb, { source, target, replaceField, replaceContent }) {
        if (!replaceContent || !replaceField) {
            this.returnMethod(cb, {
                err: new Error('replaceContent 和 replaceField 不能为空'),
            });
        } else {
            let err = null;
            try {
                source = path_.resolve(source);
                target = path_.resolve(target);
                const sourceContent = fs.readFileSync(source, 'utf8') || '';
                fs.writeFileSync(target, sourceContent.replace(replaceField, replaceContent));
            } catch (e) {
                err = e;
            } finally {
                this.returnMethod(cb, {
                    err,
                });
            }
        }
    }
}

module.exports = fileClass;
